/********************************************************************************
 * Copyright (c)2016-2020, ShenZhen WanJiaAn Technology CO.,LTD.
 * All Rights Reserved.
 *
 * File Name     : DevWifi.c
 * Author        : joki.zhu
 * Date          : 2019-10-16
 *
 * Record        :
 * 1.Date        : 2019-10-16
 *   Author      : joki.zhu
 *   Modification: Created file
 ********************************************************************************/
#include "DevWifi.h"
#include "app_common.h"

#ifdef YD_PROTOCOL
#include "include/dot_api.h"
#include "include/dot_define.h"
#endif

static int wifi_get_config(char *ssid, char *password, char *encrypt)
{
#ifdef DEV_CONFIG
    DevConfigGetWifi(ssid, password, encrypt);
    if (ssid != NULL && strlen(ssid) == 0)
    {
        if (ssid != NULL) {
            ConfigGetEnv(ENV_WIFI_TOKEN, ENV_WIFI_KEY_SSID, ssid);        
        }
        if (password != NULL) {
            ConfigGetEnv(ENV_WIFI_TOKEN, ENV_WIFI_KEY_PASSWORD, password);
        }
        if (encrypt != NULL) {
            ConfigGetEnv(ENV_WIFI_TOKEN, ENV_WIFI_ENCRYPT, encrypt);
        }
    }
#else
    if (ssid != NULL) {
        ConfigGetEnv(ENV_WIFI_TOKEN, ENV_WIFI_KEY_SSID, ssid);
    }
    if (password != NULL) {
        ConfigGetEnv(ENV_WIFI_TOKEN, ENV_WIFI_KEY_PASSWORD, password);
    }
    if (encrypt != NULL) {
        ConfigGetEnv(ENV_WIFI_TOKEN, ENV_WIFI_ENCRYPT, encrypt);
    }
#endif /* DEV_CONFIG */
    return 0;
}


/**
 * @brief WiFi初始化
 *
 * @param 无
 * 
 * @return |0|成功| |非0|失败|
 *
 * @attention 无
 */
int WifiInit(void)
{
#ifdef HISYSLINK_ENABLE
    return hi3861L_init();
#else
    return hi1131s_init();
#endif /* HISYSLINK_ENABLE */
}

/**
 * @brief 连接WiFi,并保存配置
 *
 * @param[in] ssid 热点名称
 * @param[in] pwd  热点密码
 * @param[in] enEncryptType 加密类型
 * 
 * @return |0|成功| |非0|失败|
 *
 * @attention 无
 */
int WifiSetConnect(char *ssid, char *pwd, WIFI_ENCRYPT_TYPE_E enEncryptType)
{
#ifdef HISYSLINK_ENABLE
    return hi3861L_set_connect(ssid, pwd, enEncryptType);
#else
    return hi1131s_set_connect(ssid, pwd, enEncryptType);
#endif /* HISYSLINK_ENABLE */
}

/**
 * @brief 断开WiFi连接
 *
 * @param 无
 * 
 * @return |0|成功| |非0|失败|
 *
 * @attention 无
 */
int WifiSetDisconnect(void)
{
#ifdef HISYSLINK_ENABLE
    
#else
    hi1131s_set_disconnect();
#endif /* HISYSLINK_ENABLE */
}

/**
 * @brief 连接配置中已保存的WiFi
 *
 * @param 无
 * 
 * @return |0|成功| |非0|失败|
 *
 * @attention 无
 */
int WifiSetConnectLocalWifi(void)
{
#ifdef HISYSLINK_ENABLE
    
#else
    return hi1131s_set_connect_local_wifi();
#endif /* HISYSLINK_ENABLE */
}

/**
 * @brief 获取WiFi状态
 *
 * @param 无
 * 
 * @return |0|成功| |非0|失败|
 *
 * @attention 无
 */
int WifiGetStatus(WIFI_STATUS_INFO_S *pstStatusInfo)
{
    int ret = 0;
    
#if 1
#ifdef HISYSLINK_ENABLE
    ret = hi3861L_get_status(pstStatusInfo);
    AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == 0 , AV_FAILURE, "hi3861L_get_status");
#else
    ret = hi1131s_get_status(pstStatusInfo);
    AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == 0 , AV_FAILURE, "hi1131s_get_status");
#endif /* HISYSLINK_ENABLE */
#endif

#if 0
    if (NetPing("www.baidu.com", 3, 200) == 1) {
        pstStatusInfo->connectionStatus = WIFI_CONNECTED_WITH_INTERNET;
    }
#endif

    return 0;
}

/**
 * @brief 获取周边WiFi列表信息
 *
 * @param[in] pstWifiListInfo WIFI列表信息
 * 
 * @return |0|成功| |非0|失败|
 *
 * @attention 无
 */
int WifiGetListInfo(WIFI_LIST_INFO_S *pstWifiListInfo)
{
#ifdef HISYSLINK_ENABLE
    
#else
    return hi1131s_get_list_info(pstWifiListInfo);
#endif /* HISYSLINK_ENABLE */
}

/**
 * @brief 获取WiFi连接状态
 *
 * @param 无
 * 
 * @return |1|连接| |0|未连接|
 *
 * @attention 无
 */
int WifiCheckIsConnected()
{
#ifdef HISYSLINK_ENABLE
    return hi3861L_get_link_status();
#else
    return hi1131s_get_link_status();
#endif /* HISYSLINK_ENABLE */
}

/**
 * @brief 获取WiFi唤醒原因 枚举hisi_wow_wakeup_reason_type_enum
 *
 * @param 无
 * 
 * @return |1|不处理| |0|需要设置唤醒表示为0|
 *
 * @attention 无
 */
int WifiGetWakeupReason(void)
{
#ifdef HISYSLINK_ENABLE
    
#else
    return hi1131s_get_wakeup_reason();
#endif /* HISYSLINK_ENABLE */
}


/**
 * @brief 设置默认WiFi配置
 *
 * @param 无
 * 
 * @return |0|成功| |非0|失败|
 *
 * @attention 无
 */
int WifiSetDefaultConfig(void)
{
#ifdef HISYSLINK_ENABLE
    
#else
    return hi1131s_set_default_config();
#endif /* HISYSLINK_ENABLE */
}

/**
 * @brief 检查WiFi配置是否存在
 *
 * @param 无
 * 
 * @return |1|存在| |0|不存在|
 *
 * @attention 无
 */
int WifiCheckIsExistentConfig(void)
{
    char ssid[32] = "";
    char bExistentConfig = 0;

    wifi_get_config(ssid, NULL, NULL);
    if (strlen(ssid) != 0) {
        bExistentConfig = 1;
    }

    return bExistentConfig;
}

int WifiCheckSsidIsExistent(char *ssid)
{
#ifdef HISYSLINK_ENABLE
    return 1;
#else
    return hi1131s_check_ssid_is_existent(ssid);
#endif
}

/**
 * @brief 获取WiFi休眠配置
 *
 * @param[out] pstSleepAttr WiFi休眠属性
 * 
 * @return |0|成功| |非0|失败|
 *
 * @attention 无
 */
int WifiGetSleepConfig(WIFI_SLEEP_ATTR_S *pstSleepAttr)
{
    char uuid[32] = "";
    int cnt = 0;

    DevGetUuid(uuid);
    snprintf(pstSleepAttr->heartBeatData, sizeof(pstSleepAttr->heartBeatData), 
                "{\"command\":\"300100\", \"client_id\":\"%s\"}", uuid);
    pstSleepAttr->heartBeatLen = strlen(pstSleepAttr->heartBeatData);
    strncpy(pstSleepAttr->wakeupData, "ZDKALIVE", sizeof(pstSleepAttr->wakeupData));

    snprintf(pstSleepAttr->server[cnt].ip, sizeof(pstSleepAttr->server[cnt].ip),
                "%s", "106.75.148.61");
    pstSleepAttr->server[cnt].port = 9508;
    cnt++;

#if 0 //debug
    snprintf(pstSleepAttr->server[cnt].ip, sizeof(pstSleepAttr->server[cnt].ip),
                "%s", "192.168.0.22");
    pstSleepAttr->server[cnt].port = 8888;
    cnt++;
#endif
    
    pstSleepAttr->serverNum = cnt;
    pstSleepAttr->wakeupLen = strlen(pstSleepAttr->wakeupData);
    pstSleepAttr->interval = 60; //10s
    
#ifdef ZDK_PROTOCOL
    DevGetUuid(uuid);
    snprintf(pstSleepAttr->heartBeatData, sizeof(pstSleepAttr->heartBeatData), 
                "{\"command\":\"300100\", \"client_id\":\"%s\"}", uuid);
    pstSleepAttr->heartBeatLen = strlen(pstSleepAttr->heartBeatData);
    strncpy(pstSleepAttr->wakeupData, "ZDKALIVE", sizeof(pstSleepAttr->wakeupData));

    snprintf(pstSleepAttr->server[cnt].ip, sizeof(pstSleepAttr->server[cnt].ip),
                "%s", "106.75.148.61");
    pstSleepAttr->server[cnt].port = 9508;
    cnt++;

#if 0 //debug
    snprintf(pstSleepAttr->server[cnt].ip, sizeof(pstSleepAttr->server[cnt].ip),
                "%s", "192.168.0.22");
    pstSleepAttr->server[cnt].port = 8888;
    cnt++;
#endif
    
    pstSleepAttr->serverNum = cnt;
    pstSleepAttr->wakeupLen = strlen(pstSleepAttr->wakeupData);
    pstSleepAttr->interval = 60; //10s
#elif YD_PROTOCOL
	#if 1
	DoT_KeepLiveInfo_st liveinfo;
	int i = 0;
	memset(&liveinfo, 0, sizeof(DoT_KeepLiveInfo_st));

	if(0 == DoT_GetKeepLiveInfo(&liveinfo))
	{
		printf("\n\n\n\nWifiGetSleepConfig\n\n\n");
		liveinfo.heartbeat_pkt[DOT_MAX_HEARTBEAT_PACKET_LEN - 1] = '\0';
		printf("liveinfo.heartbeat_pkt = 0x%x\n", liveinfo.heartbeat_pkt[0]);
		printf("liveinfo.heartbeat_pkt_len = %d\n", liveinfo.heartbeat_pkt_len);
		liveinfo.wakeup_pkt[DOT_MAX_WAKEUP_PACKET_LEN - 1] = '\0';
		printf("liveinfo.wakeup_pkt = 0x%x\n", liveinfo.wakeup_pkt[0]);
		printf("liveinfo.wakeup_pkt_len = %d\n", liveinfo.wakeup_pkt_len);
		printf("liveinfo.server_count = %d\n", liveinfo.server_count);
		printf("liveinfo.heartbeat_interval = %d\n", liveinfo.heartbeat_interval);
	        strcpy(pstSleepAttr->heartBeatData, liveinfo.heartbeat_pkt);
		pstSleepAttr->heartBeatLen = liveinfo.heartbeat_pkt_len;
		strcpy(pstSleepAttr->wakeupData, liveinfo.wakeup_pkt);
		pstSleepAttr->wakeupLen = liveinfo.wakeup_pkt_len;
		pstSleepAttr->serverNum = liveinfo.server_count;
		pstSleepAttr->interval = liveinfo.heartbeat_interval;
		for (i = 0; i < pstSleepAttr->serverNum; i++)
		{
			strcpy(pstSleepAttr->server[i].ip, liveinfo.server_address[i].server_ip);
			pstSleepAttr->server[i].port = liveinfo.server_address[i].server_port;
			printf("pstSleepAttr->server[%d].ip = %s\n",i,pstSleepAttr->server[i].ip);
			printf("pstSleepAttr->server[%d].port = %d\n",i,pstSleepAttr->server[i].port);
		}
	}
	#endif
#elif TELECOM

#endif
    return 0;
}

/**
 * @brief 设置WiFi休眠心跳
 *
 * @param[in] stSleepAttr WiFi休眠属性
 * 
 * @return |0|成功| |非0|失败|
 *
 * @attention 无
 */
int WifiSetSleepHeartBeat(WIFI_SLEEP_ATTR_S stSleepAttr)
{
#ifdef HISYSLINK_ENABLE
    
#else
    return hi1131s_set_sleep_heartbeat(stSleepAttr);
#endif /* HISYSLINK_ENABLE */
}

/**
 * @brief 设置WiFi进入休眠模式
 *
 * @param 无
 * 
 * @return |0|成功| |-1|创建心跳失败| |-2|不允许休眠,检测到系统正在升级|
 *
 * @attention 无
 */
int WifiSetSleepMode(void)
{
#ifdef HISYSLINK_ENABLE
    return hi3861L_set_sleep_mode();
#else
    return hi1131s_set_sleep_mode();
#endif /* HISYSLINK_ENABLE */
}


int WifiStopStaMode(void)
{
#ifdef HISYSLINK_ENABLE
        
#else
    return hi1131s_stop_sta_mode();
#endif /* HISYSLINK_ENABLE */
}

int WifiRunSta(void)
{
#ifdef HISYSLINK_ENABLE
    
#else
    return hi1131s_run_sta();
#endif /* HISYSLINK_ENABLE */
}

int WifiRunAp(WIFI_AP_CONFIG_S stApConfig)
{
#ifdef HISYSLINK_ENABLE
    
#else
    return hi1131s_run_ap(stApConfig);
#endif /* HISYSLINK_ENABLE */
}


// 预留
int WifiGetApConfig(WIFI_AP_CONFIG_S *pstApConfig)
{
    return 0;
}

/**
 * @brief 切换WiFi模式
 *
 * @param[in] stModeAttr 模式属性
 * 
 * @return |0|成功| |非0|失败|
 *
 * @attention 若切换为AP模式,需填充AP配置. 切换STA模式,暂不需要填充配置
 */
int WifiSetMode(WIFI_MODE_ATTR_S     stModeAttr)
{
#ifdef HISYSLINK_ENABLE
    
#else
    return hi1131s_set_mode(stModeAttr);
#endif /* HISYSLINK_ENABLE */
}

/**
 * @brief WIFI去初始化
 *
 * @param 无
 * 
 * @return |0|成功| |非0|失败|
 *
 * @attention 无
 */
int WifiDeinit(void)
{
#ifdef HISYSLINK_ENABLE
    return hi3861L_deinit();
#else
    return hi1131s_deinit();
#endif /* HISYSLINK_ENABLE */
}

